package com.qiche.controller.mini;

import com.qiche.aspect.Result;
import com.qiche.dto.ProductCategoryDto;
import com.qiche.enums.ResultEnum;
import com.qiche.model.ServiceItemWorkstage;
import com.qiche.model.SubscribeProduct;
import com.qiche.model.User;
import com.qiche.service.ProductCategoryService;
import com.qiche.service.ServiceItemWorkstageService;
import com.qiche.service.SubscribeProductService;
import com.qiche.util.UserUtils;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 微信小程序端服务产品管理控制器
 * Created by Linzw on 2018/01/24.
 */
@RestController
@RequestMapping("/products/mini")
public class ProductMiniController {
    @Resource
    private ProductCategoryService categoryService;
    @Resource
    private ServiceItemWorkstageService serviceItemWorkstageService;
    @Resource
    private SubscribeProductService subscribeProductService;

    /**
     * 小程序端获取所有的产品列表 (带分类)
     * @return
     */
    @GetMapping("/all")
    public Result getAllProduct() {
        User currentUser = UserUtils.getCurrentUser();
        if (currentUser == null) {
            return Result.createByResultEnum(ResultEnum.MEMBER_LOGIN_PAST);
        }
        List<ProductCategoryDto> productCategoryDtos = categoryService
                .selectProductCategoryByMerchantId(currentUser.getMerchantId().longValue());
        return Result.createByResultEnum(ResultEnum.SUCCESS, productCategoryDtos);
    }

    /**
     * 根据预约项目标识(subpro_id)获取工序列表
     * @return
     */
    @GetMapping("/getWorkStagesBySubId/{subId}")
    public Result getWorkStagesBySubId(@PathVariable("subId") Long subId) {
        List<SubscribeProduct> subscribeProducts=subscribeProductService.selectBySubscribeId(subId);
        if(subscribeProducts==null){
            return Result.createByErrorMessage("不存在的预约项目");
        }

        List<ServiceItemWorkstage> serviceItemWorkstages=serviceItemWorkstageService.selectBySubproId(subscribeProducts.get(0).getSubProId());
      return Result.createBySuccess(serviceItemWorkstages);
    }

    /**
     * 根据预约项目工序标识(id)修改工序状态
     * @return
     */
    @GetMapping("/nextWorkStage/{id}")
    public Result nextWorkStage(@PathVariable("id") Long serviceItemWorkstageId) {
        return serviceItemWorkstageService.nextWorkStage(serviceItemWorkstageId);
    }

    /**
     * 根据预约项目标识(id)开始工序
     * @return
     */
    @GetMapping("/startWorkStage/{id}")
    public Result startWorkStage(@PathVariable("id") Long subscribeId) {
        return serviceItemWorkstageService.startWorkStage(subscribeId);
    }



}
